"use strict";

const Controller = require("egg").Controller;

const data = [{
  id: 0,
  name: '番茄',
  image: 'https://s11.ax1x.com/2023/12/23/pi7rkxe.png',
  calories: 15,
  protein: 0.9,
  fat: 0.2,
  carbohydrates: 3.3,
  vitaminC: 14.0
}, {
  id: 1,
  name: '核桃',
  image: 'https://s11.ax1x.com/2023/12/23/pi7rEKH.png',
  calories: 646,
  protein: 14.9,
  fat: 58.8,
  carbohydrates: 19.1,
  vitaminC: 1.0
}, {
  id: 2,
  name: '黄瓜',
  image: 'https://s11.ax1x.com/2023/12/23/pi7DoEq.png',
  calories: 16,
  protein: 0.8,
  fat: 0.2,
  carbohydrates: 2.9,
  vitaminC: 9.0
}, {
  id: 3,
  name: '蓝莓',
  image: 'https://s11.ax1x.com/2023/12/23/pi7ri8O.png',
  calories: 57,
  protein: 0.7,
  fat: 0.3,
  carbohydrates: 14.5,
  vitaminC: 9.7
}, {
  id: 4,
  name: '螃蟹',
  image: 'https://s11.ax1x.com/2023/12/23/pi7Dvr9.png',
  calories: 97,
  protein: 19,
  fat: 1.5,
  carbohydrates: 0,
  vitaminC: 7.6
}, {
  id: 5,
  name: '猕猴桃',
  image: 'https://s11.ax1x.com/2023/12/23/pi7DbCT.png',
  calories: 61,
  protein: 0.8,
  fat: 0.6,
  carbohydrates: 14.5,
  vitaminC: 62
}, {
  id: 6,
  name: '草莓',
  image: 'https://s11.ax1x.com/2023/12/23/pi7rmVI.png',
  calories: 32,
  protein: 1.0,
  fat: 0.2,
  carbohydrates: 7.1,
  vitaminC: 47.0
}]

class FoodController extends Controller {



  async getFoodsList() {
    // const res = 
    // {
    //   'B': [{
    //     id: 3,
    //     name: '蓝莓',
    //     image: 'https://s1.ax1x.com/2023/08/02/pPPVK54.png',
    //     calories: 57,
    //     protein: 0.7,
    //     fat: 0.3,
    //     carbohydrates: 14.5,
    //     vitaminC: 9.7
    //   }],
    //   'F': [{
    //     id: 0,
    //     name: '番茄',
    //     image: 'https://s1.ax1x.com/2023/08/02/pPPVGKx.png',
    //     calories: 15,
    //     protein: 0.9,
    //     fat: 0.2,
    //     carbohydrates: 3.3,
    //     vitaminC: 14.0
    //   }],
    //   'H': [{
    //     id: 1,
    //     name: '核桃',
    //     image: 'https://s1.ax1x.com/2023/08/02/pPPDwFO.png',
    //     calories: 646,
    //     protein: 14.9,
    //     fat: 58.8,
    //     carbohydrates: 19.1,
    //     vitaminC: 1.0
    //   }, {
    //     id: 2,
    //     name: '黄瓜',
    //     image: 'https://s1.ax1x.com/2023/08/02/pPPVZrV.png',
    //     calories: 16,
    //     protein: 0.8,
    //     fat: 0.2,
    //     carbohydrates: 2.9,
    //     vitaminC: 9.0
    //   }],
    //   'P': [{
    //     id: 4,
    //     name: '螃蟹',
    //     image: 'https://s1.ax1x.com/2023/08/02/pPPVuaF.png',
    //     calories: 97,
    //     protein: 19,
    //     fat: 1.5,
    //     carbohydrates: 0,
    //     vitaminC: 7.6
    //   }]
    // }
    const token = this.ctx.cookies.get("token", {
      signed: false,
      encrypt: true
    });
    const res = data
    this.ctx.body = res;
  }


  async getFoodsDetail() {
    const { id = 0 } = this.ctx.query;
    this.ctx.body = data[id]
  }

  /**
   * 存储记录到food表的POST接口
   * 需要的请求体参数：
   * - userId: 用户ID
   * - food: 食物信息对象
   * - value: 数值
   * - type: 类型
   */
  async addFoodRecord() {
    try {
      const { id, food, value, type } = this.ctx.request.body;
      console.log(id, food, value, type);
      // 验证必要的字段
      if (!id || value === undefined || type === undefined) {
        this.ctx.body = {
          code: -1,
          msg: '缺少必要的参数(id, value, type)',
          data: null
        };
        return;
      }

      // 创建新记录
      const newRecord = await this.ctx.model.Food.create({
        userId: id,
        food: food || {}, // 如果没有提供food信息，使用空对象
        value,
        type
      });

      // 返回成功响应
      this.ctx.body = {
        code: 1,
        msg: '记录添加成功',
        data: newRecord
      };
    } catch (error) {
      // 处理错误
      console.error('添加food记录失败:', error);
      this.ctx.body = {
        code: -1000,
        msg: '服务器内部错误',
        data: null
      };
    }
  }

  /**
   * 根据id获取food列表数据
   * 通过query参数id指定用户ID
   */
  async getFoodListById() {
    try {
      const { id } = this.ctx.query;

      // 验证id参数
      if (!id) {
        this.ctx.body = {
          code: -1,
          msg: '缺少必要的参数id',
          data: null
        };
        return;
      }

      // 从数据库查询该用户的所有food记录
      const foodList = await this.ctx.model.Food.find({ userId: id });

      // 返回查询结果
      this.ctx.body = {
        data: foodList
      };
    } catch (error) {
      // 处理错误
      console.error('查询food列表失败:', error);
      this.ctx.body = {
        code: -1000,
        msg: '服务器内部错误',
        data: null
      };
    }
  }

  /**
   * 根据id删除food表中的记录
   * 需要的请求体参数：
   * - id: 记录ID
   */
  async deleteFoodRecord() {
    try {
      const { id } = this.ctx.request.body;
      console.log('====', id);
      // 验证id参数
      if (!id) {
        this.ctx.body = {
          code: -1,
          msg: '缺少必要的参数id',
          data: null
        };
        return;
      }
      console.log('====', id);
      // 从数据库删除记录
      const result = await this.ctx.model.Food.findByIdAndDelete(id);
      console.log('====', id);
      if (result) {
        // 返回成功响应
        this.ctx.body = {
          code: 1,
          msg: '记录删除成功',
          data: result
        };
      } else {
        // 没有找到要删除的记录
        this.ctx.body = {
          code: 0,
          msg: '未找到要删除的记录',
          data: null
        };
      }
    } catch (error) {
      // 处理错误
      console.error('删除food记录失败:', error);
      this.ctx.body = {
        code: -1000,
        msg: '服务器内部错误',
        data: null
      };
    }
  }
}

module.exports = FoodController;
